﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using MobileSales.Server.Services;
using MobileSales.Common.Logger;
using MobileSales.Model.Products;

namespace MobileSales.Server.RestServices
{
    public class AuthServiceREST : AuthService, IAuthServiceREST
    {
        #region Login
        public Model.Employees.EmployeeAccess Login(string userName, string passwdSHA1, string c2dmRegistrationId)
        {
            string token = base.Login(userName, passwdSHA1);
            if (token != null)
            {
                MobileSales.Server.Database.Employee employee = entities.Employees.Where(x => x.Login.Equals(userName)).FirstOrDefault();
                if (employee != null)
                {
                    employee.C2DMRegistrationId = c2dmRegistrationId;
                    entities.SaveChanges();

                    Model.Employees.EmployeeAccess access = new Model.Employees.EmployeeAccess
                    {
                        Language = employee.Language,
                        EmployeeId = employee.EmployeeId,
                        Token = token
                    };
                    return access;
                }
                else
                    Logger.LogError("cannot find employee");
            }
            return null;
        }
        #endregion

        #region ChangePassword
        public void ChangePassword(string oldPasswdSHA1, string newPasswdSHA1)
        {
            base.ChangePassword(this.GetValidationToken(), oldPasswdSHA1, newPasswdSHA1);
        } 
        #endregion
    }
}
